const gitlabApi = require('./gitlab-api');
const csvStorage = require('./csv-storage');
const { ensureHttpsUrl } = require('./utils');
const config = require('./config');

async function collectProjectsData(gitlabProjects) {
  const projectsData = [];
  
  for (const project of gitlabProjects) {
    projectsData.push({
      gitlab_id: project.id,
      name: project.name,
      name_with_namespace: project.name_with_namespace,
      path: project.path,
      path_with_namespace: project.path_with_namespace,
      web_url: ensureHttpsUrl(project.web_url),
      ssh_url_to_repo: project.ssh_url_to_repo,
      http_url_to_repo: ensureHttpsUrl(project.http_url_to_repo),
      created_at: project.created_at,
      last_activity_at: project.last_activity_at,
      visibility: project.visibility,
      default_branch: project.default_branch
    });
  }
  
  return projectsData;
}

async function collectBranchesData(gitlabProjects) {
  const branchesData = [];
  
  for (const project of gitlabProjects) {
    console.log(`正在处理项目: ${project.name_with_namespace}`);
    
    console.log(`  正在获取项目 ${project.name} 的分支列表...`);
    const branches = await gitlabApi.getProjectBranches(project.id);
    console.log(`  找到 ${branches.length} 个分支`);
    
    for (const branch of branches) {
      branchesData.push({
        gitlab_project_id: project.id,
        name: branch.name,
        merged: branch.merged,
        protected: branch.protected,
        developers_can_push: branch.developers_can_push,
        developers_can_merge: branch.developers_can_merge,
        can_push: branch.can_push,
        web_url: branch.web_url,
        commit_id: branch.commit?.id,
        commit_short_id: branch.commit?.short_id,
        commit_title: branch.commit?.title,
        commit_created_at: branch.commit?.created_at,
        commit_author_name: branch.commit?.author_name,
        commit_author_email: branch.commit?.author_email
      });
    }
  }
  
  return branchesData;
}

async function saveToCsv(projectsData, branchesData) {
  console.log('\n=== 步骤 4: 清空现有 CSV 文件 ===');
  await csvStorage.clearFiles();
  
  console.log('\n=== 步骤 5: 批量保存项目数据 ===');
  const savedProjects = await csvStorage.saveProjects(projectsData, config.storage.batchSize);
  
  const gitlabIdToProjectId = {};
  savedProjects.forEach(p => {
    gitlabIdToProjectId[p.gitlabId] = p.id;
  });
  
  console.log('\n=== 步骤 6: 批量保存分支数据 ===');
  const branchesWithProjectId = branchesData.map(branch => ({
    project_id: gitlabIdToProjectId[branch.gitlab_project_id],
    name: branch.name,
    merged: branch.merged,
    protected: branch.protected,
    developers_can_push: branch.developers_can_push,
    developers_can_merge: branch.developers_can_merge,
    can_push: branch.can_push,
    web_url: branch.web_url,
    commit_id: branch.commit_id,
    commit_short_id: branch.commit_short_id,
    commit_title: branch.commit_title,
    commit_created_at: branch.commit_created_at,
    commit_author_name: branch.commit_author_name,
    commit_author_email: branch.commit_author_email
  }));
  
  await csvStorage.saveBranches(branchesWithProjectId, config.storage.batchSize);
}

async function main() {
  try {
    await csvStorage.initialize();
    
    console.log('=== 步骤 1: 从 GitLab 获取所有项目 ===');
    const gitlabProjects = await gitlabApi.getProjects();
    console.log(`找到 ${gitlabProjects.length} 个项目`);
    
    console.log('\n=== 步骤 2: 收集项目数据 ===');
    const projectsData = await collectProjectsData(gitlabProjects);
    
    console.log('\n=== 步骤 3: 获取每个项目的所有分支 ===');
    const branchesData = await collectBranchesData(gitlabProjects);
    
    console.log(`\n=== 数据收集完成 ===`);
    console.log(`项目总数: ${projectsData.length}`);
    console.log(`分支总数: ${branchesData.length}`);
    
    await saveToCsv(projectsData, branchesData);
    
    console.log('\n=== 全部完成! ===');
  } catch (error) {
    console.error('发生错误:', error);
  } finally {
    csvStorage.close();
  }
}

main();